package com.neusoft.training.podcast.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.neusoft.training.podcast.entity.User;

public interface UserMapper {
	@Insert("insert into user(name, pwd, email, isAdmin, isDel, header) values(#{name},#{pwd},#{email},0,0,#{header})")
	void insert(User user);
	
	@Delete("delete * from user where name=#{name}")
	void cleanUser(@Param("name")String name);
	
	@Update("update user set isDel=1 where name=#{name}")
	void deleteUser(@Param("name")String name);
	
	@Update("update user set header=#{header} where userID=#{userID}")
	void updateUserHeader(@Param("header")String header,@Param("userID")Integer userID );
	
	@Update("update user set name=#{updateName}, pwd=#{updatePwd} where name=#{originalName}")
	void update(@Param("originalName")String originalName,@Param("updateName")String updateName,@Param("updatePwd")String updatePwd);
	
	@Select("select * from user where isDel=0")
	List<User> findAll();
	
	@Select("select * from user where userID=#{userID}")
	User findUserByID(@Param("userID") Integer userID);
	
	@Select("select * from user where name=#{name}")
	User findByName(@Param("name") String name);

	
	@Select("select * from user where email=#{email}")
	User findByEmail(@Param("email") String email);
	
	@Select("select * from user where name like CONCAT(CONCAT('%', #{name}), '%')")
	List<User> findUserByName(@Param("name") String name);
	
	@Select("select userID from user where name like CONCAT(CONCAT('%', #{name}), '%')")
	List<Integer> findUserIDByName(@Param("name") String name);
	
	@Update("update user set name=#{updateName}, phoneNum=#{updatePhoneNum},pwd=#{password},email=#{updateEmail} where name=#{originalName}")
	void updateAll(@Param("originalName")String originalName,@Param("updateName")String updateName,@Param("password")String password,@Param("updatePhoneNum")String updatePhoneNum,@Param("updateEmail")String updateEmail);

	@Select("select distinct * from user where userID in ( select authorID from video where videoID in (select favouriteVedioID from favourite where favouriteUserID=#{favouriteUserID}))")
	List<User> findFavouriteAuthor(@Param("favouriteUserID") Integer favouriteUserID);
	
	@Select("select distinct * from user where userID in (select commentUserID from comment where commentVideoID in (select videoID from video where authorID=#{userID}))")
	List<User> findCommentUser(@Param("userID") Integer userID);
	
	@Select("select distinct * from user where userID in (select commentUserID from comment where CommentID in (select addCommentID from comment where commentUserID=#{userID}))")
    List<User> findOtherUser(@Param("userID") Integer userID);
	
	@Select("select distinct * from user where userID in (select authorID from video where videoID in (select commentVideoID from comment where commentUserID=#{userID}))")
	List<User> findCommentVideoUser(@Param("userID") Integer userID);
}
